/**
 * @file led.c
 * @author morris (a_morris@163.com)
 * @brief
 * @version 0.1
 * @date 22022-04-02
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "led.h"
#include <stdint.h>

#include "bk8400.h"
#include "pcnt.h"
static uint32_t type = 0;
void Pcnt_IRQHandler(void)
{
  if (Pcnt_GetItStatus(PcntTO) == TRUE)
  {
    Pcnt_Cmd(FALSE);
    Pcnt_ClrItStatus(PcntTO);
//		if(led.alarm)
//		{
//			led.alarm=0;
//			alarm_led_off();
//		}
//		if(led.prepare_alarm)
//		{
//			led.prepare_alarm=0;
//			alarm_led_off();
//		}
//		if(led.run)
//		{		
//			led.run=0;
//			run_led_off();
//		}	
//		if(led.fault)
//		{
//			led.fault=0;
//			fault_led_off();
//		}	
			#if SHYS_CONFIG_OUTSIDE 
				if(led.run)
				{
					run_led_off();
          led.run                = 0;
				}
			#else 
        if (type)
        {
            alarm_led_on();
            M0P_PCNT->TOCR_f.TH = type;
            type                = 0;
            Pcnt_Cmd(TRUE);
        }
        else { alarm_led_off(); }
		  #endif
  }
}

static void led_pcnt_init(void)
{
  stc_pcnt_initstruct_t PcntInitStruct;
  DDL_ZERO_STRUCT(PcntInitStruct);

  Sysctrl_SetPeripheralGate(SysctrlPeripheralPcnt, TRUE);

  PcntInitStruct.Pcnt_S0Sel  = PcntS0PInvert;
  PcntInitStruct.Pcnt_Clk    = PcntCLKXtl;
  PcntInitStruct.Pcnt_Mode   = 0;
  PcntInitStruct.Pcnt_ClkDiv = 31;
  PcntInitStruct.Pcnt_TocrEn = TRUE;
  PcntInitStruct.Pcnt_TocrTh = 100;
  PcntInitStruct.Pcnt_Dir    = PcntDirUp;
  Pcnt_Init(&PcntInitStruct);

  Pcnt_Cmd(FALSE);

  Pcnt_ClrItStatus(PcntTO);
  Pcnt_ItCfg(PcntTO, TRUE);
  EnableNvic(PCNT_IRQn, IrqLevel3, TRUE);
}

void led_init(void)
{
//  stc_gpio_cfg_t stcGpioCfg;

//  Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);

//  DDL_ZERO_STRUCT(stcGpioCfg);
//  stcGpioCfg.enDir = GpioDirOut;
//  stcGpioCfg.enDrv = GpioDrvL;
//  stcGpioCfg.enPd  = GpioPdEnable;

//  Gpio_Init(ALARM_LED_PORT, ALARM_LED_PIN, &stcGpioCfg);

  led_pcnt_init();

//  alarm_led_off();
}

void led_alarm_on_delay(uint32_t ms)
{
  if (GetBit((uint32_t)(&(M0P_PCNT->RUN)), 0) == 0)
  {
		#if SHYS_CONFIG_OUTSIDE
		#else 
    alarm_led_on();
		#endif
    M0P_PCNT->TOCR_f.TH = ms;
		type                = 0;
    Pcnt_Cmd(TRUE);
  }
}
void leds_set_pulse_out(uint16_t t1, uint16_t t2)
{
    if (GetBit((uint32_t) (&(M0P_PCNT->RUN)), 0) == 0)
    {
        alarm_led_off();
        M0P_PCNT->TOCR_f.TH = t1;
        type                = t2;
        Pcnt_Cmd(TRUE);
    }
}

void leds_set_pulse_off(void)
{
	#if SHYS_CONFIG_OUTSIDE
	
	#else 
    Pcnt_Cmd(FALSE);
    type = 0;
    alarm_led_off();
	#endif
}

//void led_prepare_alarm_on_delay(uint32_t ms)
//{
//  if (GetBit((uint32_t)(&(M0P_PCNT->RUN)), 0) == 0)
//  {
//		led.prepare_alarm=1;
//    alarm_led_on();
//    M0P_PCNT->TOCR_f.TH = ms;
//    Pcnt_Cmd(TRUE);
//  }
//}

void led_run_on_delay(uint32_t ms)
{
  if (GetBit((uint32_t)(&(M0P_PCNT->RUN)), 0) == 0)
  {
    run_led_on();
		led.run 	=1;
    M0P_PCNT->TOCR_f.TH = ms;
    Pcnt_Cmd(TRUE);
		
  }
}
void led_fault_on_delay(uint32_t ms)
{
  if (GetBit((uint32_t)(&(M0P_PCNT->RUN)), 0) == 0)
  {
		led.fault=1;
    fault_led_on();
    M0P_PCNT->TOCR_f.TH = ms;
    Pcnt_Cmd(TRUE);
  }
}

void led_fault_off(uint32_t ms)
{
  if (GetBit((uint32_t)(&(M0P_PCNT->RUN)), 0) == 0)
  {
		Pcnt_Cmd(FALSE);
    type = 0;
    alarm_led_off();
  }
}



